<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      position: relative;
      width: 100vw;
      height: 100vh;
    }

    #modal {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 200px;
      height: 200px;
      border: 1px solid red;
    }
  </style>
</head>

<body>
  <button id="open">点击打开</button>
  <button id="close">关闭弹框</button>
  <script>
    // 使用单例模式，只创建一个modal对象
    const Modal = (function() {
      let instance = null;
      return function() {
        if (!instance) {
          let node = document.createElement('div');
          node.id = 'modal';
          node.style.display = 'none';
          node.innerHTML = '弹出一个弹层';
          document.body.append(node);
          instance = node;
        }
        return instance
      }
    })();
    document.getElementById('open').addEventListener('click', function() {
      const modal = Modal();
      modal.style.display = 'block';
    });
    document.getElementById('close').addEventListener('click', function() {
      const modal = Modal();
      modal.style.display = 'none';
    })

    // es5 装饰模式
    function openModal() {
      const modal = Modal();
      modal.style.display = 'block';
    }

    function changeButton() {
      const btn = document.getElementById('open');
      btn.innerText = '快去登录';
      btn.setAttribute('disabled', true);
    }

    document.getElementById('open').addEventListener('click', function() {
      openModal();
      changeButton();
    })
  </script>

</body>

</html>